/*
 * Copyright 2011 www.pretty-tools.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "stdafx.h"
#include "WebBrowserPersist.h"
#include "IE/WebBrowser.h"
using namespace Automation::IE;

// CWebBrowserPersist

CWebBrowserPersist::CWebBrowserPersist()
{
    ATLTRACE( _T("CWebBrowserPersist::CWebBrowserPersist()\n") );
}

CWebBrowserPersist::~CWebBrowserPersist()
{
    ATLTRACE( _T("CWebBrowserPersist::~CWebBrowserPersist()\n") );
}

STDMETHODIMP CWebBrowserPersist::get_progressListener(IDispatch** pVal)
{
    if (!pVal)
        return E_POINTER;

    *pVal = NULL;

    if (!m_pProgressListener)
        return S_OK;

    return m_pProgressListener->QueryInterface(IID_IDispatch, (void**)pVal);
}

STDMETHODIMP CWebBrowserPersist::put_progressListener(IDispatch* newVal)
{
    m_pProgressListener = newVal;

    return S_OK;
}

STDMETHODIMP CWebBrowserPersist::saveURI(BSTR uri, ILocalFile* destFile)
{
    if (!uri || !destFile)
        return E_INVALIDARG;

    VARIANT_BOOL vbSafe = VARIANT_FALSE;
    RETURN_FAILED(destFile->GetSafe(&vbSafe));

    if (VARIANT_TRUE != vbSafe)
        return E_ACCESSDENIED;

    CComBSTR bsPath;
    RETURN_FAILED(destFile->get_nativePath(&bsPath));

    CComPtr<IBindStatusCallback> pCallBack;
    if (m_pProgressListener)
    {
        CComObject<BindStatusCallback>* pInstance;
        if (SUCCEEDED(CComObject<BindStatusCallback>::CreateInstance(&pInstance)))
        {
            pInstance->AddRef();
            pInstance->m_pProgressListener = m_pProgressListener;
            pInstance->QueryInterface( __uuidof(IBindStatusCallback), (void**)&pCallBack );
            pInstance->Release();
        }
    }

    return URLDownloadToFile(NULL, uri, bsPath, 0, pCallBack);
}

STDMETHODIMP CWebBrowserPersist::BindStatusCallback::OnProgress( 
    /* [in] */ ULONG ulProgress,
    /* [in] */ ULONG ulProgressMax,
    /* [in] */ ULONG ulStatusCode,
    /* [unique][in] */ __RPC__in_opt LPCWSTR szStatusText)
{
    ATLTRACE(L"OnProgress(%i, %i, %i, %s)\n", ulProgress, ulProgressMax, ulStatusCode, szStatusText ? szStatusText : L"");

    return S_OK; //TODO: do progress and E_ABORT
}

STDMETHODIMP CWebBrowserPersist::BindStatusCallback::OnStopBinding( 
    /* [in] */ HRESULT hresult,
    /* [unique][in] */ __RPC__in_opt LPCWSTR szError)
{
    ATLTRACE(L"OnStopBinding(0x%x, %s)\n", hresult, szError ? szError : L"");

    ATLTRACE("OnStopBinding: callback(...);\n");
    JS::Method callback(m_pProgressListener);

    callback(100, 100);

    return S_OK;
}
